import { reactive, ref } from 'vue'
import message from 'ant-design-vue/lib/message/index.js'
import { SAVED_SUCCESSFULLY } from '@/constants/message.js'
import Configuration from '@/api/client/configuration.js'
export function useBlacklist(id, form) {
  const blacklistTableColumns = [
    {
      title: 'Type',
      dataIndex: 'blackListTypeName',
      key: 'blackListTypeName'
    },
    {
      title: 'Reason',
      dataIndex: 'reason',
      key: 'reason'
    },
    {
      title: 'Created By',
      dataIndex: 'displayName',
      key: 'displayName',
      customRender: ({ text }) => text || '--'
    },
    {
      title: 'Created On',
      dataIndex: 'createOn',
      key: 'createOn'
    },
    {
      title: 'Status',
      dataIndex: 'deleted',
      key: 'deleted'
    },
    {
      title: 'Operate',
      key: 'opt',
      fixed: 'right',
      width: 200,
      align: 'center'
    }
  ]
  const rules = {
    blacklistOperateReason: [
      {
        required: true,
        whitespace: true,
        message: 'Please input reason',
        trigger: 'blur'
      }
    ]
  }
  const blacklistLoading = ref(true)
  const blacklisted = ref(false)
  const showBlacklistReasonModal = ref(false)
  const blacklistedReasonList = ref([])
  const blacklistModel = reactive({
    action: 'add',
    recordId: undefined,
    blacklistOperateReason: ''
  })
  const handleBlacklistChange = () => {
    blacklistModel.action = 'add'
    blacklistModel.recordId = undefined
    blacklistModel.blacklistOperateReason = ''
    showBlacklistReasonModal.value = true
  }
  const loadBlacklistStatus = () => {
    Promise.all([Configuration.getBlacklistStatus(id), Configuration.getBlacklistedReason(id)])
      .then(([status, list]) => {
        blacklisted.value = status.data
        blacklistedReasonList.value = list.data
      })
      .finally(() => {
        blacklistLoading.value = false
      })
  }
  loadBlacklistStatus()
  const addClientToBlacklist = () => {
    form.value.validate().then(() => {
      Configuration.addClintToBlackList(id, blacklistModel.blacklistOperateReason).then(() => {
        message.success(SAVED_SUCCESSFULLY)
        showBlacklistReasonModal.value = false
        loadBlacklistStatus()
      })
    })
  }
  const revokeBlacklistRecord = id => {
    blacklistModel.action = 'revoke'
    blacklistModel.recordId = id
    blacklistModel.blacklistOperateReason = ''
    showBlacklistReasonModal.value = true
  }
  const doRevokeBlacklistRecord = () => {
    form.value.validate().then(() => {
      Configuration.revokeBlacklistRecord(id, blacklistModel.recordId, blacklistModel.blacklistOperateReason).then(
        () => {
          message.success(SAVED_SUCCESSFULLY)
          showBlacklistReasonModal.value = false
          loadBlacklistStatus()
        }
      )
    })
  }
  return {
    rules,
    blacklisted,
    blacklistedReasonList,
    blacklistTableColumns,
    revokeBlacklistRecord,
    doRevokeBlacklistRecord,
    blacklistLoading,
    showBlacklistReasonModal,
    blacklistModel,
    handleBlacklistChange,
    addClientToBlacklist
  }
}
